<?php
namespace app\common\only;

class Model extends Base {
	protected $prefix = 'Model';

	public function getModel($model = null) {
		if($model === null) {
			$model = $this->prefix;
		}
		return $this->getNoPrefix($model, function($model) {
			return loadModel($model);
		}, [$model]);
	}

	public function find($model, $id, $fun = null) {
		if(is_string($model)) {
			$name = $model . '__find__' . $id;
		} else {
			$name = str_replace(loader()->get_base(), '', get_class($model)) . '__' . $id;
		}
		$name = '_find__' . $name;
		if(is_string($model)) {
			$model = $this->getModel($model);
		}
		return $this->getNoPrefix($name, function($model, $id, $fun) {	
			if(is_callable($fun)) {
                $fun($model);
            }
			return $model->find($id);
		}, [$model, $id, $fun]);
	}

	public function lockFind($model, $id, $fun = null) {
		if(is_string($model)) {
			$name = $model . '__' . $id;
		} else {
			$name = str_replace($this->get_base() . '\\model\\', '', get_class($model));
		}
		$name = '_lockFind__' . $name;
		if(is_string($model)) {
			$model = $this->getModel($model);
		}
		return $this->getNoPrefix($name, function($model, $id, $fun) {
			if(is_callable($fun)) {
                $fun($model);
            }
			return $model->lock(true)->find($id);
		}, [$model, $id, $fun]);
	}

	public function get_row($id) {
		return $this->find($this->prefix, $id);
	}
}